package at.com.vn.chess.service;

import java.util.ArrayList;
import at.com.vn.chess.entity.ChatUser;
import at.com.vn.chess.entity.User;
import vn.com.bip.office.smart.util.XMLHelper;

public class ChatPublicService extends BaseService {

	public ChatPublicService(String inputStrXml) {
		super(inputStrXml);
		logger.info( "Begin chat public service" );
	}

	@SuppressWarnings("unchecked")
	@Override
	public void doService() {

		// Create initation xml error message
		String errMessage = "Some error";
		outputStrXml = XMLHelper.convertMessageToXML("ChatPublic", "ERR_MSG",
				"MSG", errMessage);

		if (inputStrXml.trim().equals("")) {			
			logger.error( "Client truyen len mot file inputStrXml rong" );
			return;
		}

		String processName = XMLHelper.getProcessName(inputStrXml);
		logger.info( "Get process name: " + processName );
		// return all current user
		if (processName.contains("_getListUser")) {
			outputStrXml = XMLHelper.convertListObjectToXML(chatUserList,
					processName, ChatUser.class);
		} else {

			// Return chat user
			// get user
			ArrayList<ChatUser> receiverLstUser = (ArrayList<ChatUser>) XMLHelper
					.convertXMLToListObject(inputStrXml, ChatUser.class);

			if (processName.contains("_join")) {
				if (!isAvailable(receiverLstUser.get(0), chatUserList)) {
					chatUserList.add(receiverLstUser.get(0));
				} else {
					remove(receiverLstUser.get(0), chatUserList);
					chatUserList.add(receiverLstUser.get(0));
				}
				// send data
				outputStrXml = getMXLResult(inputStrXml, chatUserList);
				// Add more ChatUser infor
				// not join any room, use when remove user when close browser
				if (!receiverLstUser.get(0).getRoomID().equals("")) {
					AddInfo();
				}
			}

			// send data
			outputStrXml = getMXLResult(inputStrXml, chatUserList);
			logger.info( "Gui ve server chuoi xml: " + outputStrXml );
		}

	}

	private void AddInfo() {
		logger.info( "AddInfo function -> Bat dau" );
		for (ChatUser iChatUser : chatUserList) {
			for (User iUser : loginUserList) {
				if (iUser.getUser_id().equals(iChatUser.getUserID())) {
					iChatUser.setWin(iUser.getWin());
					iChatUser.setLoss(iUser.getLoss());
					iChatUser.setRank_id(iUser.getRank_id());
					iChatUser.setGold(iUser.getGold());
					break;
				}
			}
		}
	}

	private boolean isAvailable(ChatUser user, ArrayList<ChatUser> lstUser) {
		logger.info( "isAvailable function -> Bat dau" );
		boolean isAvail = false;
		for (ChatUser iuser : lstUser) {
			if (iuser.getUserID().equals(user.getUserID())) {
				isAvail = true;
				break;
			}
		}
		return isAvail;
	}

	private void remove(ChatUser user, ArrayList<ChatUser> lstUser) {
		logger.info( "remove function -> Bat dau" );
		for (ChatUser iuser : lstUser) {
			if (iuser.getUserID().equals(user.getUserID())) {
				lstUser.remove(iuser);
				break;
			}
		}
	}

	private String getMXLResult(String xmlInfo, ArrayList<ChatUser> lstUser) {
		logger.info( "getMXLResult function -> Bat dau" );
		ChatUser user = (ChatUser) XMLHelper.convertXMLToListObject(xmlInfo,
				ChatUser.class).get(0);
		String processName = XMLHelper.getProcessName(xmlInfo);

		// Reset other user's status
		for (ChatUser otherUser : lstUser) {
			if (!user.getUserID().equals(otherUser.getUserID())) {
				// Do not show chat message for other user
				otherUser.setIsActive(0);
			} else {
				// copy client user's info
				otherUser.setUserID(user.getUserID());
				otherUser.setRoomID(user.getRoomID());
				otherUser.setChatMsg(user.getChatMsg());
				// Show chat message on client
				otherUser.setIsActive(1);

				// Prepare data for chat public
				if (processName.contains("_left")) {
					otherUser.setIsDeleted(1);
				} else {
					otherUser.setIsDeleted(0);
				}
			}

		}

		// Broadcast it to all listening clients
		return XMLHelper.convertListObjectToXML(lstUser, processName,
				ChatUser.class);
	}
}